import { _decorator, Component, Node, Label, director, AudioClip, Sprite, Color } from 'cc';
const { ccclass, property } = _decorator;

import { SSS_Consts } from "../../../../Script/Data/SSS_Consts";
import { bdlMgr } from "../../../../Script/Manager/BundleManager_S";
import { Minesweeper_GameData } from "../Data/Minesweeper_GameData";
import Minesweeper_GameManager from "../Manager/Minesweeper_GameManager";
import { SSS_AudioManager } from 'db://assets/Script/Manager/SSS_AudioManager';

@ccclass('MinesweeperGamePanel')
export default class Minesweeper_GamePanel extends Component {
    static Instance: Minesweeper_GamePanel;
    @property(Node)
    Button_back: Node | null = null;
    @property(Node)
    Button_refresh: Node | null = null;
    @property(Label)
    Label_score: Label | null = null;
    @property(Label)
    Label_highScore: Label | null = null;
    @property(Label)
    Label_mines: Label | null = null;

    @property(Node)
    扫雷: Node = null;
    @property(Node)
    插旗: Node = null;

    GeneratePoint_blook_list = [];
    GeneratePoint_blook_list_temp = [];

    onLoad() {
        Minesweeper_GamePanel.Instance = this;
    }
    ShowOn() {
        this.node.active = true;
        this.AddListener();
        this.Refresh();
    }
    ShowOff() {
        this.node.active = false;
        this.RemoveListener();
    }
    Refresh() {
        this.Refresh_score();
        this.Refresh_mines();
        this.Refresh_modeUI();
    }
    Refresh_score() {
        this.Label_score.string = `本局得分：${Minesweeper_GameData.GetScore()}`;
        // this.Label_highScore.string = `最高纪录：${S2048_GameStorage.data.high_score}`;
    }
    Refresh_mines() {
        // 计算剩余雷数量 = 总雷数 - 已插旗数
        let flagged = 0;
        for (let row = 0; row < Minesweeper_GameData.row; row++) {
            for (let col = 0; col < Minesweeper_GameData.col; col++) {
                const grid = Minesweeper_GameData.arrGrids[row]?.[col];
                if (grid && grid.是否插旗) flagged++;
            }
        }
        const remain = Minesweeper_GameData.雷数量 - flagged;
        if (this.Label_mines) {
            this.Label_mines.string = `${remain}`;
        }
    }
    Refresh_modeUI() {
        // 假设扫雷和插旗按钮有不同的高亮/选中样式
        if (!this.扫雷 || !this.插旗) return;
        if (Minesweeper_GameData.状态 === "扫雷") {
            this.扫雷.getComponent(Sprite).color = new Color().fromHEX(`#73CB39`); // 选中高亮色
            this.插旗.getComponent(Sprite).color = new Color().fromHEX(`#525963`);  // 未选中
        } else if (Minesweeper_GameData.状态 === "插旗") {
            this.扫雷.getComponent(Sprite).color = new Color().fromHEX(`#525963`);
            this.插旗.getComponent(Sprite).color = new Color().fromHEX(`#73CB39`);
        } else {
            this.扫雷.getComponent(Sprite).color = new Color().fromHEX(`#525963`);
            this.插旗.getComponent(Sprite).color = new Color().fromHEX(`#525963`);
        }
    }
    AddListener() {
        this.Button_back.on(Node.EventType.TOUCH_END, this.On_Button_back, this);
        this.Button_refresh.on(Node.EventType.TOUCH_END, this.On_Button_refresh, this);

        this.扫雷.on(Node.EventType.TOUCH_END, this.On_扫雷, this);
        this.插旗.on(Node.EventType.TOUCH_END, this.On_插旗, this);
    }
    RemoveListener() {
        this.Button_back.off(Node.EventType.TOUCH_END, this.On_Button_back, this);
        this.Button_refresh.off(Node.EventType.TOUCH_END, this.On_Button_refresh, this);
    }
    On_扫雷() {
        Minesweeper_GameData.状态 = `扫雷`;
        this.Refresh_modeUI();
        console.log(`Minesweeper_GameData.状态`, Minesweeper_GameData.状态);

        bdlMgr.getRes<AudioClip>(SSS_Consts.BundleName.AUDIO, `audio/按钮音效`, AudioClip).then(res => {
            if (res) {
                SSS_AudioManager.Instance.playSFX(res, false);
            }
        });
    }

    On_插旗() {
        Minesweeper_GameData.状态 = `插旗`;
        this.Refresh_modeUI();
        console.log(`Minesweeper_GameData.状态`, Minesweeper_GameData.状态);

        bdlMgr.getRes<AudioClip>(SSS_Consts.BundleName.AUDIO, `audio/按钮音效`, AudioClip).then(res => {
            if (res) {
                SSS_AudioManager.Instance.playSFX(res, false);
            }
        });
    }
    On_Button_back() {
        director.loadScene("MainScene");

        SSS_AudioManager.PlayButtonAudio();
    }
    On_Button_refresh() {
        // 调用GameManager的重置方法，而不是重新加载场景
        if (Minesweeper_GameManager.Instance) {
            Minesweeper_GameManager.Instance.resetGame();
        } else {
            // 备用方案：如果GameManager不存在，则重新加载场景
            let currentSceneName = director.getScene().name;
            director.loadScene(currentSceneName);
        }
        this.Refresh_mines();
        this.Refresh_modeUI();
        SSS_AudioManager.PlayButtonAudio();
    }
}

// /** 调试 */
window["Minesweeper_GamePanel"] = Minesweeper_GamePanel;